package com.nowcoder.controller;

import com.nowcoder.model.HostHolder;
import com.nowcoder.model.Message;
import com.nowcoder.model.User;
import com.nowcoder.model.ViewObject;
import com.nowcoder.service.MessageService;
import com.nowcoder.service.UserSevice;
import com.nowcoder.util.ToutiaoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.View;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class MessageController {

    private static final Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    HostHolder hostHolder;

    @Autowired
    MessageService messageService;
    @Autowired
    UserSevice userSevice;

    @RequestMapping(value = {"/msg/list"}, method = RequestMethod.GET)
    public String getMsgList(Model model) {

        int localUserId = hostHolder.getUser().getId();
        List<Message> conversationList = messageService.getConversationList(localUserId, 0, 10);
        List<ViewObject> vos = new ArrayList<>();
        for (Message msg : conversationList) {
            ViewObject vo = new ViewObject();
            vo.set("conversation", msg);
            int targetId = msg.getFromId() == localUserId ? msg.getToId() : msg.getFromId();
            User user = userSevice.getUser(targetId);
            vo.set("headUrl", user.getHeadUrl());
            vo.set("userName", user.getName());
            vo.set("targetId", targetId);
            vo.set("totalCount", msg.getId());
            vo.set("unreadCount", messageService.getUnReadCount(localUserId, msg.getConversationId()));
            vos.add(vo);
        }
        model.addAttribute("conversations", vos);
        return "letter";
    }

    @RequestMapping(value = {"/msg/detail"}, method = RequestMethod.GET)
    public String getMsgDetails(Model model, @RequestParam("conversationId") String conversationId) {
        List<Message> conversationList = messageService.getConversationDetail(conversationId, 0, 10);
        List<ViewObject> vos = new ArrayList<>();
        for (Message msg : conversationList) {
            ViewObject vo = new ViewObject();
            vo.set("message", msg);
            User user = userSevice.getUser(msg.getFromId());
            if (user == null) {
                continue;
            }
            vo.set("headUrl", user.getHeadUrl());
            vo.set("userName", user.getName());
            vos.add(vo);
        }
        model.addAttribute("message", vos);
        return "letterDetail";
    }

    @RequestMapping(value = {"/msg/addMessage"}, method = RequestMethod.POST)
    @ResponseBody
    public String addMessage(@RequestParam("fromId") int fromId,
                             @RequestParam("toId") int toId,
                             @RequestParam("content") String content) {
        Message msg = new Message();
        msg.setContent(content);
        msg.setCreatedDate(new Date());
        msg.setFromId(fromId);
        msg.setToId(toId);
        msg.setConversationId(fromId < toId ? String.format("%d_%d", fromId, toId) : String.format("%d_%d", toId, fromId));
        messageService.addMessage(msg);
        return ToutiaoUtil.getJSONString(msg.getId());
    }
}
